package com.greatwall.hip.cms.controller;

import com.google.common.collect.Lists;
import com.greatwall.hip.cms.entity.LyDiseaseDimension;
import com.greatwall.hip.cms.entity.LyDiseaseTag;
import com.greatwall.hip.cms.entity.LyPneumoniaDimension;
import com.greatwall.hip.cms.model.*;
import com.greatwall.hip.cms.service.LyDiseaseDimensionService;
import com.greatwall.hip.cms.service.LyDiseaseTagService;
import com.greatwall.hip.cms.service.LyPneumoniaDimensionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * @ClassName DiseaseDrillDownController
 * @Description 重大疫情监控-疾病下钻页面
 * @Author zhous
 * @Date 2021/7/7 10:38
 */
@RestController
@RequestMapping("/epidemicMonitor/drillDown")
@Api(tags = "重大疫情监控-疾病下钻页面", value = "重大疫情监控-疾病下钻页面")
public class DiseaseDrillDownController extends BaseController {

    @Autowired
    private LyDiseaseTagService lyDiseaseTagService;

    @Autowired
    private LyDiseaseDimensionService lyDiseaseDimensionService;

    @Autowired
    private LyPneumoniaDimensionService lyPneumoniaDimensionService;

    @PostMapping("/targetDiseaseDetail")
    @ApiOperation("指定疾病患病情况")
    public Result<TargetDiseaseDetailResp> targetDiseaseDetail(@RequestBody DiseaseReq diseaseReq){

        //指定疾病的人数-按日期分组
        List<LyDiseaseTag> targetDisease = lyDiseaseTagService.queryTargetDiseaseByBetweenDate(diseaseReq);
        //所有疾病的人数-按日期分组
        DiseaseReq param = new DiseaseReq();
        param.setEndDate(diseaseReq.getEndDate());
        param.setStartDate(diseaseReq.getStartDate());
        param.setType(diseaseReq.getType());
        List<LyDiseaseTag> allDisease = lyDiseaseTagService.queryTargetDiseaseByBetweenDate(param);

        //初始化就诊总人次数
        int patientCount = 0;
        //初始化患病人次数
        int targetPatientCount = 0;
        //初始化患病率集合
        List<Double> morbidityList = Lists.newArrayList();
        List<String> dateList = rangeDate(diseaseReq);
        for (String date : dateList) {

            LyDiseaseTag target = targetDisease.stream().filter(d -> date.equals(d.getDates())).findFirst().orElse(null);
            LyDiseaseTag all = allDisease.stream().filter(d -> date.equals(d.getDates())).findFirst().orElse(null);
            int targetCount = null == target ? 0 : target.getCountOfDiseases();
            int allCount = null == all ? 0 : all.getCountOfDiseases();

            patientCount += allCount;
            targetPatientCount += targetCount;
            //计算当日的患病率
            double targetDayMorbidity = allCount == 0 ? 0d : new BigDecimal(targetCount).divide(new BigDecimal(allCount), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100")).doubleValue();
            morbidityList.add(targetDayMorbidity);
        }

        double morbidity = patientCount == 0 ? 0d : new BigDecimal(targetPatientCount).divide(new BigDecimal(patientCount), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100")).doubleValue();

        TargetDiseaseDetailResp resp = new TargetDiseaseDetailResp();
        resp.setPatientCount(patientCount);
        resp.setTargetPatientCount(targetPatientCount);
        resp.setDateList(dateList);
        resp.setMorbidity(morbidity);
        resp.setMorbidityList(morbidityList);

        return Result.success(resp);
    }

    @PostMapping("/generalDetail")
    @ApiOperation("总体疾病详情")
    public Result<GeneralDetailResp> generalDetail(@RequestBody DiseaseReq req){

        //查询指定疾病时间段内各维度的数据聚合
        LyDiseaseDimension diseaseDimension = lyDiseaseDimensionService.selectTargetSumByDatesDuration(req);
        LyPneumoniaDimension pneumoniaDimension = lyPneumoniaDimensionService.selectSumByDatesDuration(req);
        List<LyPneumoniaDimension> pneumoniaList = lyPneumoniaDimensionService.selectByDatesDuration(req);
        List<String> dateList = rangeDate(req);

        if( null == diseaseDimension ){
            diseaseDimension = new LyDiseaseDimension();
        }
        if( null == pneumoniaDimension ){
            pneumoniaDimension = new LyPneumoniaDimension();
        }

        //初始化返回实体
        GeneralDetailResp generalDetailResp = new GeneralDetailResp();
        int lungInflammationCount = pneumoniaDimension.getAllCount();
        int patientCount = diseaseDimension.getAllCount();
        int radiographicExamCount = pneumoniaDimension.getAllExamCount();
        int anomalousExamCount = pneumoniaDimension.getAnomalousExamCount();

        //检查异常人次数集合初始化
        List<Integer> anomalousExamCountList = Lists.newArrayList();
        //初始化提示肺部炎症患者人次数变化趋势集合
        List<Integer> lungInflammationCountList = Lists.newArrayList();


        for( String date : dateList ){

            LyPneumoniaDimension pneumoniaData = pneumoniaList.stream().filter(d -> date.equals(d.getDates())).findFirst().orElse(null);
            if( null == pneumoniaData ){
                lungInflammationCountList.add(0);
                anomalousExamCountList.add(0);
            }else {
                lungInflammationCountList.add(pneumoniaData.getAllCount());
                anomalousExamCountList.add(pneumoniaData.getAnomalousExamCount());
            }

        }

        generalDetailResp.setDateList(dateList);

        generalDetailResp.setLungInflammationCount(lungInflammationCount);
        generalDetailResp.setAnomalousExamCount(anomalousExamCount);
        generalDetailResp.setRadiographicExamCount(radiographicExamCount);
        double anomalousExamCountPercent = 0 == radiographicExamCount ? 0d : new BigDecimal(anomalousExamCount).divide(new BigDecimal(radiographicExamCount),4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100")).doubleValue();
        generalDetailResp.setAnomalousExamCountPercent(anomalousExamCountPercent);
        double lungInflammationCountPercent = 0 == patientCount ? 0d : new BigDecimal(lungInflammationCount).divide(new BigDecimal(patientCount),4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100")).doubleValue();
        generalDetailResp.setLungInflammationCountPercent(lungInflammationCountPercent);

        generalDetailResp.setAnomalousExamCountList(anomalousExamCountList);
        generalDetailResp.setLungInflammationCountList(lungInflammationCountList);


        generalDetailResp.setAgeTitleList(Lists.newArrayList("0-6周岁","7-14周岁","15-18周岁","19-35周岁","36-64周岁","65周岁及以上"));
        generalDetailResp.setAgePatientCountList(Lists.newArrayList(diseaseDimension.getChildhoodCount(),diseaseDimension.getSchoolageCount(),diseaseDimension.getPubertyCount(),diseaseDimension.getYouthCount(),diseaseDimension.getMiddleCount(),diseaseDimension.getOldCount()));
        generalDetailResp.setAgeLungInflammationCountList(Lists.newArrayList(pneumoniaDimension.getChildhoodCount(),pneumoniaDimension.getSchoolageCount(),pneumoniaDimension.getPubertyCount(),pneumoniaDimension.getYouthCount(),pneumoniaDimension.getMiddleCount(),pneumoniaDimension.getOldCount()));

        generalDetailResp.setSexTitleList(Lists.newArrayList("提示肺部炎症患者人次数","患病人次数"));
        generalDetailResp.setSexManList(Lists.newArrayList(pneumoniaDimension.getManCount(),diseaseDimension.getManCount()));
        generalDetailResp.setSexWomanList(Lists.newArrayList(pneumoniaDimension.getWomanCount(),diseaseDimension.getWomanCount()));
        generalDetailResp.setSexUnknowList(Lists.newArrayList(pneumoniaDimension.getUnknowCount(),diseaseDimension.getUnknowCount()));
        generalDetailResp.setSexUnstatedList(Lists.newArrayList(pneumoniaDimension.getUnstatedCount(),diseaseDimension.getUnstatedCount()));

        return Result.success(generalDetailResp);
    }
}
